library(dplyr)


x <- c(1,2,3,4)
y <- c(1,2,3,4)


combins <- expand.grid(x=x, y=y)
combins$prob[combins$x==1] <- c(1/20, 1/20, 1/20, 0)
combins$prob[combins$x==2] <- c(1/20, 2/20, 2/20, 1/20)
combins$prob[combins$x==3] <- c(1/20, 3/20, 3/20, 1/20)
combins$prob[combins$x==4] <- c(0, 1/20, 1/20, 1/20)
combins$z <- combins$x + 2 * combins$y

print(class(combins))

combins2 <- combins %>%
  group_by(z) %>%
  summarise(prob = sum(prob), m = prob * 20, t = paste(m, '/', '20'))

p_xy <- matrix(c(
  1/20, 1/20, 1/20, 0,
  1/20, 2/20, 3/20, 1/20,
  1/20, 2/20, 3/20, 1/20,
  0, 1/20, 1/20, 1/20
), nrow=length(x), byrow=F)



sum_vals <- outer(x, 2*y, '+')

sum_vec <- as.vector(sum_vals)
props <- as.vector(p_xy)

result <- aggregate(props, by=list(Sum=sum_vec), FUN=sum)
colnames(result) <- c('Value', 'Probability')

print(result, digits=4)
